package cdu.sl.controller;

import cdu.sl.anno.OperateLog;
import cdu.sl.pojo.Dept;
import cdu.sl.pojo.Result;
import cdu.sl.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
public class DeptController {

    private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    /**
     * 获取所有部门的信息
     *
     * @return List<Dept>
     */
    @GetMapping()
    public Result findAll() {
        // 调用服务层方法来获取数据
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 删除部门 ：省略@RequestParam , 但要保证前端请求的参数名和服务端方法形参名一致 [推荐]
     *
     * @param id
     * @return
     */
    @OperateLog
    @DeleteMapping()
    public Result delete(Integer id) {
        log.info("根据ID删除部门：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     */
    @OperateLog
    @PostMapping()
    public Result add(@RequestBody Dept dept) {
//        System.out.println("新增部门" + dept);
        log.info("新增部门{}", dept);
        deptService.add(dept);
        return Result.success();
    }


    /**
     * 根据id查询单个部门信息
     *
     * @param deptId
     * @return
     */
    @GetMapping("/{deptId}")
    public Result getInfo(@PathVariable Integer deptId) {
        System.out.println("根据Id查询部门:" + deptId);
        log.info("根据Id查询部门:{}", deptId);
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);
    }

    /**
     * 修改部门信息
     *
     * @param dept
     * @return
     */
    @OperateLog
    @PutMapping()
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门{}", dept);
        deptService.update(dept);
        return Result.success();
    }

}
